/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EirSummary.java
 *
 * Created on Nov 7, 2010, 9:32:12 PM
 */

package lmclogistics;

import components.Image;
import components.Lightbox;
import components.NButton;
import components.Text;
import components.TextArea;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import sql.Sql;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sonia Tan
 */
public class EirSummary extends javax.swing.JPanel {

    /** Creates new form EirSummary */
    private Image bg;
    private JTable eirSummaryTable;
    private JScrollPane scroll;
    public DefaultTableModel model;
    private TableColumn column;
    private NButton print;
    private String sDate, eDate;


    public EirSummary(String sDate, String eDate) {
        initComponents();
        setOpaque(false);
        setLocation(getCenterX(761), getCenterY(460));
        setSize(761, 460);
        setVisible(true);

        this.sDate = sDate;
        this.eDate = eDate;

        setBg();
        initTable();
        showTable();
        initButtons();
    }

    public final void setBg(){
        bg = new Image("panelb.png",761,460);

        add(bg,-1);

    }

    public int getCenterX(int x){

        int a = 919/2 - x/2;


        return a;
    }

    public int getCenterY(int x){

        int a = 577/2 - x/2;


        return a;
    }

    public void initTable(){
                eirSummaryTable = new JTable(model);
		eirSummaryTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		eirSummaryTable.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
		eirSummaryTable.getTableHeader().setFont(new Font("Lucida Grande", Font.PLAIN, 12));
		scroll = new JScrollPane(eirSummaryTable);
		scroll.setBounds(20,15,721,400);
		add(scroll,0);
    }

    public void showTable(){
        try{
            String query = "SELECT * FROM rental WHERE outDate >='"+sDate+"' AND outDate <='"+eDate+"' AND rentStatus = 'in'";

            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            model = new DefaultTableModel(){
			    public boolean isCellEditable(int row, int column) {
			    	return false;
				}
			};
                        model.addColumn("Chassis");
                        model.addColumn("Customer");

                        model.addColumn("OUT");
			model.addColumn("CWS No.");
			model.addColumn("Date");
			model.addColumn("Time");
			model.addColumn("Container No.");
			model.addColumn("Trucking");
                        model.addColumn("Plate No.");
                        model.addColumn("Taken By");
                        model.addColumn("License No.");
                        model.addColumn("Consignee");
                        model.addColumn("Remarks");

                        model.addColumn("IN");
			model.addColumn("CWS No.");
			model.addColumn("Date");
			model.addColumn("Time");
			model.addColumn("Container No.");
			model.addColumn("Trucking");
                        model.addColumn("Plate No.");
                        model.addColumn("Taken By");
                        model.addColumn("License No.");
                        model.addColumn("Consignee");
                        model.addColumn("Remarks");

               while(rs.next()) {
                   Vector rowData = new Vector();

                   rowData.add(rs.getString("chsCode"));
                   rowData.add(rs.getString("custID")); //change to name of customer
                   rowData.add(rs.getString("outCode"));
                   rowData.add(rs.getString("outCwsNo"));
                   rowData.add(rs.getString("outDate"));
                   rowData.add(rs.getString("outTime"));
                   rowData.add(rs.getString("outContainer"));
                   rowData.add(rs.getString("outTrkCode"));
                   rowData.add(rs.getString("outPltNo"));
                   rowData.add(rs.getString("outTakenBy"));
                   rowData.add(rs.getString("outLicNo"));
                   rowData.add(rs.getString("outConsignee"));
                   rowData.add(rs.getString("outRemarks"));

                   rowData.add(rs.getString("inCode"));
                   rowData.add(rs.getString("inCwsNo"));
                   rowData.add(rs.getString("inDate"));
                   rowData.add(rs.getString("inTime"));
                   rowData.add(rs.getString("inContainer"));
                   rowData.add(rs.getString("inTrkCode"));
                   rowData.add(rs.getString("inPltNo"));
                   rowData.add(rs.getString("inTakenBy"));
                   rowData.add(rs.getString("inLicNo"));
                   rowData.add(rs.getString("inConsignee"));
                   rowData.add(rs.getString("inRemarks"));

                   model.addRow(rowData);
            }

            eirSummaryTable.setModel(model);

            column = eirSummaryTable.getColumnModel().getColumn(5);
            column.setPreferredWidth(150);
            column = eirSummaryTable.getColumnModel().getColumn(6);
            column.setPreferredWidth(150);


        } catch (Exception ex) {
            Logger.getLogger(EirSummary.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void initButtons(){
        print = new NButton("Print", 580, 422);
        print.setSize(80,25);
        add(print,0);

        buttonlisteners();
    }

    public void buttonlisteners()
    {
        print.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae)
            {
                try {
                    eirSummaryTable.print();
                } catch(Exception e)
                {

                }
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
